package com.cqjtu.store.cart.controller;

import com.cqjtu.store.cart.pojo.dto.CartDTO;
import com.cqjtu.store.cart.service.CartService;
import com.cqjtu.store.common.JwtUtils;
import com.cqjtu.store.common.R;
import com.cqjtu.store.product.pojo.vo.ShoppingCartVO;
import com.cqjtu.store.user.pojo.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api/cart")
@Slf4j
@Api(tags = "购物车模块")
public class CartController {

    @Autowired
    private CartService cartService;

    @PostMapping("buy")
    public R<Void> buy(@RequestBody CartDTO[] carts, HttpServletRequest request){
        log.debug("获取到的购物车中的数据:{}",carts);
        return R.ok();
    }
    @PutMapping("updateNum")
    public R<Void> updateNum(@RequestBody CartDTO cartDTO, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        cartService.updateNum(cartDTO,userLoginVO);
        return R.ok();
    }

    @GetMapping("list")
    @ApiOperation("查询购物车列表")
    public R<List<ShoppingCartVO>> getShoppingCartList(HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        List<ShoppingCartVO> shoppingCartVOList = cartService.getShoppingCartList(userLoginVO);
        return R.ok(shoppingCartVOList);
    }

    @DeleteMapping("delete")
    @ApiOperation("删除购物车商品")
    public R<Void> deleteProductInCart(@RequestBody CartDTO[] carts,HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        cartService.deleteCarts(carts,userLoginVO);
        return R.ok();
    }

    @PutMapping("add/{id}")
    @ApiOperation("添加购物车中商品的购买数量")
    public R<Void> addNum(@PathVariable Integer id, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        cartService.addNum(id,userLoginVO);
        return R.ok();
    }

    @PutMapping("reduce/{id}")
    @ApiOperation("添加购物车中商品的购买数量")
    public R<Void> reduceNum(@PathVariable Integer id, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        cartService.reduceNum(id,userLoginVO);
        return R.ok();
    }

    @PostMapping("purchase")
    @ApiOperation("将商品加入购物车")
    public R<Void> pruchase(@RequestBody CartDTO cartDTO, HttpServletRequest request){
        //获取用户的身份
        String token = request.getHeader("Authorization");
        UserLoginVO userLoginVO = JwtUtils.parseToken(token);
        //加购物车
        cartService.purchase(cartDTO,userLoginVO);
        return R.ok();
    }

}
